package org.tao.lightningmq.nameserver.common;

import io.netty.channel.Channel;
import org.tao.lightningmq.common.dto.NodeAckDTO;
import org.tao.lightningmq.common.dto.SlaveAckDTO;
import org.tao.lightningmq.nameserver.core.PropertiesLoader;
import org.tao.lightningmq.nameserver.enums.ReplicationModeEnum;
import org.tao.lightningmq.nameserver.replication.BaseReplicationTask;
import org.tao.lightningmq.nameserver.store.ReplicationChannelManager;
import org.tao.lightningmq.nameserver.store.ReplicationMsgQueueManager;
import org.tao.lightningmq.nameserver.store.ServiceInstanceManager;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author lht
 * @date 2025/1/8 - 14:26
 * @description:
 */
public class CommonCache {

    private static ServiceInstanceManager serviceInstanceManager = new ServiceInstanceManager();
    private static NameserverProperties nameserverProperties;
    private static PropertiesLoader propertiesLoader = new PropertiesLoader();
    private static Map<String, SlaveAckDTO> ackMap = new ConcurrentHashMap<>();
    private static Channel connectNodeChannel = null;
    private static Channel preNodeChannel = null;
    private static ReplicationMsgQueueManager replicationMsgQueueManager = new ReplicationMsgQueueManager();
    private static Map<String, NodeAckDTO> nodeAckMap = new ConcurrentHashMap<>();
    private static ReplicationModeEnum replicationMode;

    public static BaseReplicationTask getReplicationTask() {
        return replicationTask;
    }

    public static void setReplicationTask(BaseReplicationTask replicationTask) {
        CommonCache.replicationTask = replicationTask;
    }

    private static BaseReplicationTask replicationTask;

    public static Map<String, NodeAckDTO> getNodeAckMap() {
        return nodeAckMap;
    }

    public static ReplicationMsgQueueManager getReplicationMsgQueueManager() {
        return replicationMsgQueueManager;
    }

    public static Channel getPreNodeChannel() {
        return preNodeChannel;
    }

    public static void setPreNodeChannel(Channel preNodeChannel) {
        CommonCache.preNodeChannel = preNodeChannel;
    }

    public static Channel getConnectNodeChannel() {
        return connectNodeChannel;
    }

    public static void setConnectNodeChannel(Channel connectNodeChannel) {
        CommonCache.connectNodeChannel = connectNodeChannel;
    }

    public static ReplicationChannelManager getReplicationChannelManager() {
        return replicationChannelManager;
    }

    private static ReplicationChannelManager replicationChannelManager = new ReplicationChannelManager();

    public static PropertiesLoader getPropertiesLoader() {
        return propertiesLoader;
    }

    public static void setNameserverProperties(NameserverProperties nameserverProperties) {
        CommonCache.nameserverProperties = nameserverProperties;
    }

    public static ServiceInstanceManager getServiceInstanceManager() {
        return serviceInstanceManager;
    }

    public static NameserverProperties getNameserverProperties() {
        return nameserverProperties;
    }

    public static Map<String, SlaveAckDTO> getAckMap() {
        return ackMap;
    }

    public static void setReplicationMode(ReplicationModeEnum replicationMode) {
        CommonCache.replicationMode = replicationMode;
    }

    public static ReplicationModeEnum getReplicationMode() {
        return replicationMode;
    }
}
